package com.cn.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cn.bean.City;
import com.cn.bean.Provider;
import com.cn.bean.SecondKill;
import com.cn.bean.Servicecatalog;
import com.cn.dao.ProviderDAO;
import com.cn.dao.SecondKillDAO;
import com.cn.dao.ServicecatalogDAO;
import com.cn.entity.SecondKillEntity;
import com.cn.service.SecondKillService;

public class SecondKillServiceImpl implements SecondKillService{
	private SecondKillDAO secondkillDao=null;
	private ServicecatalogDAO servicecatalogDao=null;
	private ProviderDAO providerDao=null;

	public ServicecatalogDAO getServicecatalogDao() {
		return servicecatalogDao;
	}

	public void setServicecatalogDao(ServicecatalogDAO servicecatalogDao) {
		this.servicecatalogDao = servicecatalogDao;
	}

	public ProviderDAO getProviderDao() {
		return providerDao;
	}

	public void setProviderDao(ProviderDAO providerDao) {
		this.providerDao = providerDao;
	}

	public SecondKillDAO getSecondkillDao() {
		return secondkillDao;
	}

	public void setSecondkillDao(SecondKillDAO secondkillDao) {
		this.secondkillDao = secondkillDao;
	}

	/**
	 * 获取全部的秒杀活动
	 * @throws Exception 
	 */
	public List getSecondKills() throws Exception{
		List list=secondkillDao.findAll();
		List<SecondKillEntity> list1=new ArrayList<SecondKillEntity>();
		for(int i=0;i<list.size();i++){
			SecondKill sk=(SecondKill)list.get(i);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateString=sdf.format(sk.getTime());
			SecondKillEntity ske=new SecondKillEntity(sk.getId(),sk.getServicecatalog().getId(),sk.getProvider().getId(),sk.getServicecatalog().getName(),sk.getNum(),dateString);
			list1.add(ske);
		}
		return list1;
	}
	
	/**
	 * 二级联动返回服务商
	 * @param parentId
	 * @param level
	 * @return
	 */
	public ArrayList<Map<String, Object>> getcatalogProvider(String parentId, String level){
		String hql;
		List<Object> list = null;
		Map<String, Object> map = null;
		ArrayList<Map<String, Object>> rsList = new ArrayList<Map<String, Object>>();
		if (level.equals("servicecatalog")) {
			
			list = servicecatalogDao.listAllCatalog(5);
			Servicecatalog servicecatalog;
			for (int i = 0; i < list.size(); i++) {
				map = new HashMap<String, Object>();
				servicecatalog = (Servicecatalog) list.get(i);
				map.put("id", servicecatalog.getId());
				map.put("name", servicecatalog.getName());
				rsList.add(map);
			}
		} else {
			int sc_id=Integer.parseInt(parentId);
			list = providerDao.findBySC(sc_id,1);
			Provider provider;
			for (int i = 0; i < list.size(); i++) {
				map = new HashMap<String, Object>();
				provider = (Provider) list.get(i);
				map.put("id",provider.getId());
				map.put("name",provider.getName() );
				rsList.add(map);
			}
		}
		return rsList;
	}
	
	/**
	 * 写入数据库
	 * @param secondKill
	 * @return
	 */
	public boolean setSecondKill(SecondKill secondKill){
		if(secondkillDao.save(secondKill)){
			return true;
		}
		else{
			return false;
		}
	}
}
